package com.xiaode.controller.callBack;

import java.time.LocalDateTime;

import com.xiaode.common.constant.Const;
import com.xiaode.common.model.BehaviorUserLog;
import com.xiaode.common.model.Camera;
import com.xiaode.common.model.MonitorClassroomUser;
import com.xiaode.common.model.UserAccount;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class HikvisionReturnService {

	
	

	//是不是可以用JSONUtil.getByPath()解析
	public void hikvisionReturn(String data){
		if(StrUtil.isBlank(data) && !JSONUtil.isTypeJSONObject(data)) {
			return;
		}
		JSONObject returnData = JSONUtil.parseObj(data);
		JSONObject params = returnData.getJSONObject("params");
		String ability = params.getStr("ability");
		if(StrUtil.equals("event_face", ability)) {//人脸抓拍
			System.err.println("人脸抓拍");
		}else if(StrUtil.equals("event_face_recognition", ability)) {//重点人员识别
			faceMatch(params);
			System.err.println("重点人员识别");
		}else if(StrUtil.equals("event_rule", ability)) {//行为
			System.err.println("行为");
		}
	}
	
	/**
	 * 识别后业务处理
	 */
	public void faceMatch(JSONObject params){
		String cameraIndexCode = params.getByPath("events[0].data.targetAttrs.cameraIndexCode", String.class);
		String faceInfoCode = params.getByPath("events[0].data.faceRecognitionResult.faceMatch[0].faceInfoCode", String.class);
		
		Camera camera = Camera.dao.findFirst("SELECT * FROM camera WHERE camera_code = ?", cameraIndexCode);
		if(BeanUtil.isEmpty(camera)) {
			return;
		}
		//改变对应教室里的人并添加进出日志
		UserAccount userAccount = UserAccount.dao.findFirst("SELECT id,user_type FROM user_account WHERE face_code = ?", faceInfoCode);
		if(BeanUtil.isEmpty(userAccount)){
			return;
		}
		//获取教室里的这个学生
		MonitorClassroomUser monitorClassroomUser = MonitorClassroomUser.dao.findFirst("SELECT id FROM monitor_classroom_user WHERE account_id = ?", userAccount.getId());
		if(camera.getUseType() == null) {
			return;
		}
		if(camera.getUseType() == Const.cameraUseType.ENTER) {//进
			if(BeanUtil.isEmpty(monitorClassroomUser)) {
				monitorClassroomUser = new MonitorClassroomUser();
				monitorClassroomUser.setMonitorClassroomId(camera.getMonitorClassroonId());
				monitorClassroomUser.setAccountId(userAccount.getId());
				monitorClassroomUser.setUserType(userAccount.getUserType());
				monitorClassroomUser.save();
			}
			//添加进出日志
			BehaviorUserLog behaviorUserLog = new BehaviorUserLog();
			behaviorUserLog.setMonitorClassroomId(camera.getMonitorClassroonId());
			behaviorUserLog.setAccountId(userAccount.getId());
			behaviorUserLog.setUserType(userAccount.getUserType());
			behaviorUserLog.setBehaviorType(Const.behaviorType.ENTER);
			behaviorUserLog.setAddTime(LocalDateTime.now());
			behaviorUserLog.save();
		}
		if(camera.getUseType() == Const.cameraUseType.EXIT) {//出
			if(BeanUtil.isNotEmpty(monitorClassroomUser)) {
				monitorClassroomUser.delete();
			}
			//添加进出日志
			BehaviorUserLog behaviorUserLog = new BehaviorUserLog();
			behaviorUserLog.setMonitorClassroomId(camera.getMonitorClassroonId());
			behaviorUserLog.setAccountId(userAccount.getId());
			behaviorUserLog.setUserType(userAccount.getUserType());
			behaviorUserLog.setBehaviorType(Const.behaviorType.EXIT);
			behaviorUserLog.setAddTime(LocalDateTime.now());
			behaviorUserLog.save();
		}
		
	}
	
	
	
}
